package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.PinLunHomeBean;
import com.lwj.liwanjia.bean.WorkBean09;
import com.lwj.liwanjia.utils.BaseUtils;
import com.lwj.liwanjia.utils.MyGridView;
import com.lwj.liwanjia.utils.RatingBar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 001 on 2017/9/13.
 */

public class PinLunHomeAdapter extends BaseAdapter {

    private Context mContext;
    private List<WorkBean09.DataBean> mList;
    private HuifuListener listener;

    public void setListener(HuifuListener listener) {
        this.listener = listener;
    }

    public PinLunHomeAdapter(Context mContext, List<WorkBean09.DataBean> mList) {
        this.mContext = mContext;
        this.mList = mList;

    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_pin_lun_home, parent, false);
            holder.circle_photo = (CircleImageView) convertView.findViewById(R.id.circle_photo);
            holder.mGridView = (MyGridView) convertView.findViewById(R.id.gridView);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.shangjia_huifu = (RelativeLayout) convertView.findViewById(R.id.shangjia_huifu);
            holder.shangjia_huifu_time = (TextView) convertView.findViewById(R.id.shangjia_huifu_time);
            holder.shangjia_huifu_content = (TextView) convertView.findViewById(R.id.shangjia_huifu_content);
            holder.zhuijia_content = (TextView) convertView.findViewById(R.id.zhuijia_content);
            holder.zhuijia_pingjia = (RelativeLayout) convertView.findViewById(R.id.zhuijia_pingjia);
            holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar);
            holder.ivHuifu = (ImageView) convertView.findViewById(R.id.iv_huifu);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        WorkBean09.DataBean bean = mList.get(position);

        Glide.with(mContext).load(bean.getHeadImg()).error(R.mipmap.touxiang).into(holder.circle_photo);
        holder.tv_name.setText(bean.getNick());
        holder.tv_content.setText(bean.getContent());

        holder.ratingBar.setStar((float) bean.getGrade());

        long time = bean.getTime();
        String timeResult = BaseUtils.getString(String.valueOf(time / 1000));
        holder.tv_time.setText(timeResult);

        PinLunPhotoAdapter adapter = new PinLunPhotoAdapter(mContext, bean.getImgs());
        holder.mGridView.setAdapter(adapter);
        if (TextUtils.isEmpty(bean.getAddContent())) {
            holder.zhuijia_pingjia.setVisibility(View.GONE);
        } else {
            holder.zhuijia_pingjia.setVisibility(View.VISIBLE);
            holder.zhuijia_content.setText(bean.getAddContent());
        }
        if (TextUtils.isEmpty(bean.getReplyInfo())) {
            holder.shangjia_huifu.setVisibility(View.GONE);
        } else {
            holder.shangjia_huifu.setVisibility(View.VISIBLE);
            holder.shangjia_huifu_content.setText(bean.getReplyInfo());
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
            String date = dateFormat.format(bean.getReplyTime());
            holder.shangjia_huifu_time.setText(date);
        }

        if (listener != null) {
            holder.ivHuifu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.huifu(v, position);
                }
            });
        }
        return convertView;
    }

    class ViewHolder {

        CircleImageView circle_photo;
        ImageView ivHuifu;
        MyGridView mGridView;
        TextView tv_name;
        TextView tv_content;
        TextView tv_time;
        RelativeLayout zhuijia_pingjia;
        TextView zhuijia_content;
        RatingBar ratingBar;
        RelativeLayout shangjia_huifu;
        TextView shangjia_huifu_time;
        TextView shangjia_huifu_content;
    }


    public interface HuifuListener {
        void huifu(View view, int position);
    }
}
